package com.kye.express.business.online_pay.detail;

import android.content.Context;
import android.text.TextUtils;

import com.kye.express.R;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.RxView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.online.GetPayRecordDetailsResultBean;
import com.kye.model.bean.online.request.RequestGetPayRecordDetailsBean;
import com.kye.model.bean.user.User;
import com.kye.model.service.OnlinePayService;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface OnlinePayDetailFragmentContract {

    interface OnlinePayDetailFragmentView extends RxView {

        void onComplete();

        void onLoading();

        void onLoaded(GetPayRecordDetailsResultBean datas);

        void onApiError(String msg);

        void onOtherError(String msg);
    }

    class OnlinePayDetailFragmentViewPresenter extends BasePresenter<OnlinePayDetailFragmentContract.OnlinePayDetailFragmentView> {

        private Context mContext;

        public OnlinePayDetailFragmentViewPresenter(Context context) {
            this.mContext = context;
        }

        /**
         * 查询支付详情
         */
        public void getPayDetail(String waybill) {
            String phone = "";
            User user = UserSession.getInstance().getUser();
            if (user != null) {
                phone = user.getPhone();
            }

            RetrofitManager.getInstance().createOpenApiService(OnlinePayService.class)
                    .getPayRecordDetail(new RequestGetPayRecordDetailsBean(waybill, phone))
                    .compose(getView().<OpenApiResult<GetPayRecordDetailsResultBean>>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<GetPayRecordDetailsResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onLoaded() {
                            if (getView() != null) {
                                getView().onComplete();
                            }
                        }

                        @Override
                        public void onSuccess(OpenApiResult<GetPayRecordDetailsResultBean> result, GetPayRecordDetailsResultBean datas) {
                            if (datas != null) {
                                getView().onLoaded(datas);
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<GetPayRecordDetailsResultBean> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onApiError(msg);
                            } else {
                                getView().onOtherError(mContext.getString(R.string.common_net_error));
                            }
                        }

                    });
        }


    }
}
